import 'package:flutter/material.dart';

import '../../config/public_color.dart';
import '../../foundation/decoration/border_decoration.dart';
import '../container/clip_radius_container.dart';
import '../container/radius_container.dart';
import '../container/separated_row.dart';
import '../divider/public_vertical_divider.dart';

/// FileName public_segment
///
/// @Author lax
/// @Date 2023/7/4
///
/// @Description

class PublicSegment extends StatelessWidget {
  static TextStyle? defaultSelectedTextStyle = const TextStyle(
      fontSize: 14, fontWeight: FontWeight.w500, color: Colors.white);

  static TextStyle? defaultUnselectedTextStyle;

  static EdgeInsets? defaultItemPadding =
      const EdgeInsets.symmetric(horizontal: 8, vertical: 4);

  static Color? defaultSelectedColor;

  static Color? defaultUnselectedColor;

  static Color? defaultBorderColor;

  final int? value;

  final List<String> dataSource;

  /// 选中的字体
  late final TextStyle? selectedTextStyle;

  /// 未选中的字体
  late final TextStyle? unselectedTextStyle;

  /// 选中的背景颜色
  late final Color? selectedColor;

  /// 未选中的背景颜色
  late final Color? unselectedColor;

  /// 边框颜色
  late final Color? borderColor;

  late final EdgeInsets? itemPadding;

  final double? itemWidth;

  final EdgeInsets? margin;

  final Function(int index)? onChanged;

  final bool? flexible;

  PublicSegment({
    super.key,
    this.value,
    required this.dataSource,
    TextStyle? selectedTextStyle,
    TextStyle? unselectedTextStyle,
    Color? selectedColor,
    Color? unselectedColor,
    Color? borderColor,
    EdgeInsets? itemPadding,
    this.itemWidth,
    this.margin,
    this.onChanged,
    this.flexible = false,
  }) {
    this.selectedTextStyle = selectedTextStyle ?? defaultSelectedTextStyle;
    this.unselectedTextStyle = unselectedTextStyle ??
        defaultUnselectedTextStyle ??
        TextStyle(fontSize: 14, color: borderColor ?? PublicColor.primary);
    this.selectedColor =
        selectedColor ?? defaultSelectedColor ?? PublicColor.primary;
    this.unselectedColor =
        unselectedColor ?? defaultUnselectedColor ?? Colors.transparent;
    this.borderColor = borderColor ?? defaultBorderColor ?? PublicColor.primary;
    this.itemPadding = itemPadding ?? defaultItemPadding ?? EdgeInsets.zero;
  }

  @override
  Widget build(BuildContext context) {
    if (dataSource.isEmpty) {
      return const SizedBox();
    }
    if (flexible == true) {
      return ClipRadiusContainer(
        margin: margin,
        padding: const EdgeInsets.all(2),
        decoration: BorderDecoration(radius: 5, borderColor: borderColor),
        radius: 5,
        child: SeparatedRow(
          expanded: true,
          separator: const SizedBox(width: 2),
          children: children(),
        ),
      );
    }
    return ClipRadiusContainer(
      margin: margin,
      decoration: BorderDecoration(radius: 5, borderColor: borderColor),
      radius: 5,
      child: UnconstrainedBox(
        child: IntrinsicHeight(
          child: SeparatedRow(
            separator: PublicVerticalDivider(width: 1, color: borderColor),
            children: children(),
          ),
        ),
      ),
    );
  }

  List<Widget> children() {
    List<Widget> list = [];
    for (var i = 0; i < dataSource.length; i++) {
      list.add(InkWell(
        onTap: () {
          onChanged?.call(i);
        },
        child: RadiusContainer(
          radius: flexible == true ? 5 : 0,
          color: i == value ? selectedColor : unselectedColor,
          padding: itemPadding,
          width: itemWidth,
          alignment: Alignment.center,
          child: Text(
            dataSource[i],
            style: i == value ? selectedTextStyle : unselectedTextStyle,
          ),
        ),
      ));
    }
    return list;
  }
}
